Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Parsimmon is a library for writing simple, elegant parsers in JavaScript. It allows you to construct parsers for custom languages or data formats using a combinator-based approach.
Basic Parsing
This feature allows you to create a basic parser that matches a specific string. In this example, the parser is set to match the string 'hello'.
const P = require('parsimmon');
const parser = P.string('hello');
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: false, expected: ['hello'], index: { offset: 0, line: 1, column: 1 } }
Sequence Parsing
This feature allows you to create a parser that matches a sequence of patterns. In this example, the parser matches the sequence 'hello', whitespace, and 'world'.
const P = require('parsimmon');
const parser = P.seq(P.string('hello'), P.whitespace, P.string('world'));
console.log(parser.parse('hello world')); // { status: true, value: ['hello', ' ', 'world'] }
console.log(parser.parse('hello there')); // { status: false, expected: ['world'], index: { offset: 6, line: 1, column: 7 } }
Custom Parsers
This feature allows you to create custom parsers using regular expressions and combinators. In this example, the parser matches a sequence of digits.
const P = require('parsimmon');
const digit = P.regexp(/[0-9]/).desc('a digit');
const digits = digit.many().map(digits => digits.join(''));
console.log(digits.parse('12345')); // { status: true, value: '12345' }
console.log(digits.parse('abc')); // { status: false, expected: ['a digit'], index: { offset: 0, line: 1, column: 1 } }
Error Handling
This feature allows you to handle parsing errors gracefully by providing alternative parsing options. In this example, the parser matches either 'hello' or 'world'.
const P = require('parsimmon');
const parser = P.string('hello').or(P.string('world'));
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: true, value: 'world' }
console.log(parser.parse('foo')); // { status: false, expected: ['hello', 'world'], index: { offset: 0, line: 1, column: 1 } }
Nearley is a fast, feature-rich parser toolkit for JavaScript. It uses a different approach called Earley parsing, which can handle more complex grammars compared to Parsimmon. Nearley is more powerful but can be more complex to use.
PEG.js is a simple parser generator for JavaScript based on the Parsing Expression Grammar (PEG) formalism. It allows you to define your grammar in a concise syntax and generates a parser from it. PEG.js is more declarative compared to Parsimmon's combinator-based approach.
Jison is a parser generator that converts a context-free grammar into a JavaScript parser. It is similar to tools like Bison or Yacc but for JavaScript. Jison is more suitable for complex language parsing tasks compared to Parsimmon.
Authors: @jneen and @laughinghan
Maintainer: @wavebeem
Parsimmon is a small library for writing big parsers made up of lots of little parsers. The API is inspired by parsec and Promises/A+.
Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm (named parsimmon
), or directly in the browser through a script tag, where it exports a global variable called Parsimmon
. To download the latest browser build, use the unpkg version. For more information on how to use unpkg, see the unpkg homepage.
Everyone is expected to abide by the Contributor Covenant. Please send reports to brian@wavebeem.com.
See the examples directory for annotated examples of parsing JSON, Lisp, a Python-ish language, and math.
.createLanguage(parsers)
.string(string)
.regexp(regexp)
.seq(p1, p2, ...pn)
.sepBy(content, separator)
.alt(p1, p2, ...pn)
.whitespace
.index
parser.map(fn)
parser.node(name)
parser.skip(otherParser)
Feel free to ask a question by filing a GitHub Issue. I'm happy to help point you in the right direction with the library, and hopefully improve the documentation so less people get confused in the future.
Contributions are not just pull requests.
Issues clearly describing bugs or confusing parts of Parsimmon are welcome! Also, documentation enhancements and examples are very desirable.
Feeling overwhelmed about contributing? Open an issue about what you want to contribute and I'm happy to help you through to completion!
Thanks to @bd82 we have a good benchmark comparing Parsimmon CPU performance to several other parser libraries with a simple JSON parser example.
Parsimmon is also compatible with fantasyland. It implements Semigroup, Apply, Applicative, Functor, Chain, and Monad.
1.18.1 (2021-12-19)
FAQs
A monadic LL(infinity) parser combinator library
The npm package parsimmon receives a total of 118,602 weekly downloads. As such, parsimmon popularity was classified as popular.
We found that parsimmon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.